<?php

declare (strict_types=1);

namespace app\store\controller\agent;


use app\store\controller\Controller;
use think\App;
use think\response\Json;
use app\store\service\agent\Promoter as PromoterService;

/**
 * 推广员管理
 * Class Setting
 * @package app\store\controller
 */
class Promoter extends Controller
{
    private $promoterService;
    public function __construct(App $app,PromoterService $promoterService)
    {
        parent::__construct($app);
        $this->promoterService = $promoterService;
    }

    /**
     * 推广员列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer',
                'channel_id|渠道ID' => 'integer',
                'user_id|推广员ID' => 'integer',
               // 'nick_name|推广员昵称' => '',
               // 'mobile|推广员手机号码' => ''
            ]
        );
        $list = $this->promoterService->getList($params);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 添加、修改推广员
     * @return array|mixed
     */
    public function save():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'channel_id|渠道id' => 'require',
                'divide|返利比例' => 'require|integer',
                'nick_name|推广员昵称' => 'require',
                'username|用户名' => 'require',
            ]
        );
        if(empty($params['id'])){
            //新增
            $this->validate(
                $params,
                [
                    'mobile|手机号码' => 'require',
                    'username|用户名' => 'require',
                    'password|密码' => 'require',
                    'confirm_password|确认密码' => 'require',
                ]
            );
        }
        $review_username_check = $this->promoterService->getReviewUsernameCheck($params['username'],$params['id'] ?? '');
        if(!$review_username_check){
            return $this->renderError('用户名重复');
        }
        $res = $this->promoterService->save($params);
        if(!$res){
            return $this->renderError('保存失败');
        }
        return $this->renderSuccess([],'保存成功');

    }


    /**
     * 删除推广员
     * @param int $categoryId
     * @return array
     * @throws \Exception
     */
    public function delete():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            ['id' => 'require|integer']
        );
        $res = $this->promoterService->delete($params['id']);
        if(!$res){
            return $this->renderError('删除异常');
        }
        return $this->renderSuccess([],'删除成功');
    }


    /**冻结操作
     * @return Json
     */
    public function frozenWithDraw():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'require|integer',
                'is_with_draw' => 'require'
            ]
        );
        $res = $this->promoterService->getFrozenWithDraw($params);
        return $this->renderSuccess([],'操作成功');
    }

}
